package com.study.www.web;

import com.alibaba.fastjson.JSONObject;
import com.study.www.config.Result;
import com.study.www.config.WebAdvice;
import com.study.www.entity.DeptVo;
import com.study.www.entity.UserVo;
import com.study.www.enums.ValidateParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/dept")
@Validated
@Slf4j
public class DeptController {

    @Autowired
    UserController userController;
    static Map<String,DeptVo> dbs = new HashMap<>();

    @PutMapping("/{id}")
    public Result put(@PathVariable(value = "id")    @Pattern(regexp = ValidateParams.validateStr, message = "id不正确")  String id,
                      @Validated(value = {DeptVo.Update.class}) DeptVo deptVo){
        System.out.println(JSONObject.toJSONString(deptVo));
        DeptVo deptVo1 = dbs.get(id);
        if (deptVo1 == null){
            return Result.error("该部门不存在!");
        }else{
            if (StringUtils.isNotBlank(deptVo.getName())) {
                deptVo1.setName(deptVo.getName());
            }
            if (deptVo.getStartSalary() != null){
                deptVo1.setStartSalary(deptVo.getStartSalary());
            }
            if (deptVo.getEndSalary() != null){
                deptVo1.setEndSalary(deptVo.getEndSalary());
            }
            if (deptVo.getOrderNo() != null) {
                deptVo1.setOrderNo(deptVo.getOrderNo());
            }
            dbs.put(id,deptVo1);
        }
        return Result.success();
    }


    @PostMapping(value = {"/",""})
    public Result post(@RequestBody @Validated(value = {DeptVo.Add.class})
                                   DeptVo deptVo, ModelMap modelMap){
        String id = modelMap.get(WebAdvice.ID).toString();
        deptVo.setId(id);
        if (deptVo.getOrderNo() == null) {
            deptVo.setOrderNo(dbs.size());
        }
        if (deptVo.getUserVoList() != null){
            deptVo.getUserVoList().stream().forEach(userVo -> {
                userVo.setDeptId(id);
                userController.save(userVo);
            });
        }
        dbs.put(id,deptVo);
        return Result.success();
    }

    @GetMapping(value = {"/{id}","/",""})
    public Result get(@PathVariable(value = "id",required = false)
                          @Pattern(regexp = ValidateParams.validateStr, message = "id不正确")  String id){
        if (StringUtils.isNotBlank(id)){
            DeptVo deptVo = dbs.get(id);
            return Result.success(deptVo);
        }
        return Result.success(dbs.values());
    }

    @DeleteMapping(value = {"/{id}","/",""})
    public Result delete(@PathVariable(value = "id",required = false)
                      @Pattern(regexp = ValidateParams.validateStr, message = "id不正确")  String id){
        if (StringUtils.isNotBlank(id)){
            dbs.remove(id);
            return Result.success();
        }
        dbs = new HashMap<>(10);
        return Result.success();
    }
}
